package com.api.syh56.shengyunhui.controller;


import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.entity.Routes;
import com.api.syh56.shengyunhui.entity.dto.RoutesDTO;
import com.api.syh56.shengyunhui.service.RoutesService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 常用路线
 */
@RestController
@RequestMapping("routes")
public class RoutesController {

    @Autowired
    private RoutesService routesService;

    /**
     * 常用路线插入
     * @param routesDTO
     * @return
     */

    @PostMapping("insert")
    public CommonResult<Object> addNewAddress(@RequestBody RoutesDTO routesDTO){
        routesService.addNewRoutes(routesDTO);
        return CommonResult.success(routesDTO);
    }

    /**
     * 删除
     * @param routes
     * @return
     */
    @PostMapping("delete")
    public CommonResult<Object> delete(@RequestBody Routes routes){
        routesService.removeRoutes(routes);
        return CommonResult.success(null);
    }

    /**
     * 更新
     * @param routes
     * @return
     */
    @PostMapping("update")
    public CommonResult<Object> update(@RequestBody Routes routes){
        routesService.updateRoutes(routes);
        return CommonResult.success(null);
    }


    /**
     * 分页查询
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping (value = "selectPageByuid",method = RequestMethod.POST)
    public CommonResult<CommonPage<Routes>>  selectPageByuid(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        Page<Routes> routesPage = routesService.selectPageByuid(uid, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(routesPage));
    }
}
